#!C:\python25\python.exe

import sys
import elementtree.ElementTree as ET
from time import strftime


class xml2html:
   def __init__(self,xmlFileName, output = "Browser"): # output: "Browser" means to browser, otherwise output Filename 
      self.OriFile = xmlFileName
      if output == "Browser":
         self.DesFile = sys.stdout  # redirection file output to system stdout
         print "Content-Type: text/html\n\n" 
      else:
         self.DesFile = open(output, "w")
      self.Parse()
      
   def Parse(self):         
      root = ET.parse(self.OriFile)
      iter = root.getiterator()
      self.OpenHtml()
      
      for element in iter:
          self.elementCheck(element)
             
      self.CloseHtml()
   
   def elementCheck(self,element):
      if element.tag == "Info":
           self.isInfo(element)
      if element.tag == "Label":
           self.isLabel(element)
      if element.tag == "Textinput":
           self.isTextinput(element)
      if element.tag == "Textarea":
           self.isTextarea(element)
            
   def OpenHtml(self):
      self.DesFile.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
      self.DesFile.write("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n")
      self.DesFile.write("<head>\n")
      self.DesFile.write("<meta http-equiv=\"content-type\" content=\"text/html; charset= utf-8\" />\n")
      self.DesFile.write("<title>Handover System</title>\n")
      self.DesFile.write("<style type=\"text/css\" media=\"screen\">\n")
      self.DesFile.write("\t/* <![CDATA[ */\n")
      self.DesFile.write("\t@import url('/handovericu/css/template.css');\n")
      self.DesFile.write("\t/* ]]> */\n")
      self.DesFile.write("</style>\n")
      self.DesFile.write("<style type=\"text/css\" media=\"print\">\n")
      self.DesFile.write("\t/* <![CDATA[ */\n")
      self.DesFile.write("\t@import url('/handovericu/css/template.css');\n")
      self.DesFile.write("\t/* ]]> */\n")
      self.DesFile.write("</style>\n")      
      self.DesFile.write("</head>\n")
      self.DesFile.write("<body>\n")      
      #self.DesFile.write("<div>\n")
      self.DesFile.write("<table class=\"istable\">\n\t<tr>\n\t\t<td>\n") 
      self.DesFile.write("\t\t<div class=\"istitle\">"+ "Handovers System" + "</div>\n")
      time = strftime("%d/%m/%Y-%H:%M:%S")
      self.DesFile.write("\t\t<div class=\"istime\">"+ time)
      
   def Title(self,classname,title):
      self.DesFile.write("\t\t<div class=\""+classname+"\">"+ title + "</div>\n")
      
   def CloseHtml(self):
      self.DesFile.write("\t\t</td>\n\t</tr>\n</table>\n</div>\n")
      self.DesFile.write("<A HREF=\"javascript:window.print()\">Click to Print This Page</A>")
      self.DesFile.write("</body></html>")
      
   def isLabel(self, element):
      self.DesFile.write("\t\t<ib class =\"islabel\"><label>"+ element.text +"</label></ib>\n");

   def isTextinput(self, element):
      for child in element.getchildren():
         if child.tag == "Value":
		if child.text is not None:
	            self.DesFile.write("\t\t<ib class=\"isinfo\">"+child.text.strip() + "</ib>\n")
        	
   def isInfo(self, element):
      if element.keys():
         for name, value in element.items():             
            self.DesFile.write("\t\t</div>\n\t\t</td>\n\t</tr>\n\t<tr>\n\t\t<td>\n" ) #\t\t<div id=\""+ value+ "\"</div>\n
            self.DesFile.write("\t\t<div class=\"isibtitle\">"+ value + "</div>\n\t\t<div class=\"isifblock\">\n") 
      
   def isTextarea(self, element):
      for child in element.getchildren():
         if child.tag == "Value":
            if child.text is not None:
               self.DesFile.write("<p class=\"isnote\">" + child.text.strip() + "</p>\n")
